package com.bnzj.cloud.business.core.web;

import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.bnzj.cloud.business.core.persistence.entity.SysDictionaryType;
import com.bnzj.cloud.business.core.service.SysDictionaryTypeService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bnzj.core.action.ActionType;
import com.bnzj.core.action.annotation.Action;
import com.bnzj.core.exception.BusinessException;
import com.bnzj.core.persistence.query.QueryHelpBean;
import com.bnzj.core.persistence.util.EntityUtils;
import com.bnzj.core.rest.CurrentUserService;
import com.bnzj.core.rest.ResponseResult;
import com.bnzj.core.rest.ResultCode;
import com.bnzj.core.rest.UserInfo;
import com.bnzj.core.util.BeanUtils;
import com.bnzj.core.webmvc.util.WebMvcUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @author Barry
 * @createDate 2020/2/14
 * @description
 */
@Tag(name = "字典数据类型", description = "字典类型管理")
@RestController
@RequestMapping("/system/dictionary/type")
public class SysDictionaryTypeController {

    @Autowired
    private CurrentUserService gatewayCurrentUserService;

    @Autowired
    private SysDictionaryTypeService sysDicTypeService;

    @Operation(summary = "分页查询", description = "")
    @GetMapping(value = "/page")
    public ResponseResult<Page<SysDictionaryType>> getDictionaryTypePage(HttpServletRequest request) {
        Pageable pageable = WebMvcUtils.initPage(request, Sort.by(Sort.Direction.DESC, "create_time"));
        QueryHelpBean dicQueryBean = SysDictionaryTypeService.getQueryHelpBean();
        dicQueryBean.setPageable(pageable);
        Page<SysDictionaryType> page = sysDicTypeService.findAll(dicQueryBean);
        return ResponseResult.success(page);
    }

    @Operation(summary = "根据id查询")
    @GetMapping("/one/{id}")
    public ResponseResult<SysDictionaryType> getById(@Parameter(in = ParameterIn.PATH, name = "id", description = "主键", required = true) @PathVariable("id") Long id) {
        Optional<SysDictionaryType> optional = sysDicTypeService.findById(id);
        return ResponseResult.success(optional.orElse(null));
    }

    @Operation(summary = "根据code查询")
    @GetMapping("/code/{code}")
    public ResponseResult<SysDictionaryType> getByCode(@Parameter(in = ParameterIn.PATH, name = "code", description = "code", required = true) @PathVariable("code") String code) {
        SysDictionaryType sysDictionaryType = new SysDictionaryType();
        sysDictionaryType.setCode(code);
        Optional<SysDictionaryType> optional = sysDicTypeService.findOne(sysDictionaryType);
        return ResponseResult.success(optional.orElse(null));
    }

    @Operation(summary = "保存")
    @Action(descriprion = "字典类型保存",moduleName = "字典类型",operateType = ActionType.ADD_DATA)
    @PostMapping("/save")
    public ResponseResult<?> addDictionaryType(@RequestBody
                                               @Valid SysDictionaryType sysDictionaryType) {
        UserInfo userInfo = gatewayCurrentUserService.getUserInfo();
        EntityUtils.saveBefore(sysDictionaryType, userInfo.getAccount(), userInfo.getName());
        sysDicTypeService.save(sysDictionaryType);
        return ResponseResult.success();
    }

    @Operation(summary = "更新")
    @Action(descriprion = "字典类型更新",moduleName = "字典类型",operateType = ActionType.UPDATE_DATA)
    @PostMapping("/update")
    public ResponseResult<?> updateDictionaryType(@RequestBody SysDictionaryType sysDictionaryType) {
        if (ObjectUtils.isEmpty(sysDictionaryType.getId())) {
            throw new BusinessException(ResultCode.getResultCode(ResultCode.BAD_REQUEST));
        }
        UserInfo userInfo = gatewayCurrentUserService.getUserInfo();
        Optional<SysDictionaryType> sysDepartmentOpt = sysDicTypeService.findById(sysDictionaryType.getId());
        if (!sysDepartmentOpt.isPresent()) {
            throw new BusinessException(ResultCode.getResultCode(ResultCode.RESOURCE_NOT_EXIST));
        }
        SysDictionaryType sysDictionaryTypeDb = sysDepartmentOpt.get();
        BeanUtils.copyProperties(sysDictionaryType, sysDictionaryTypeDb);
        EntityUtils.saveBefore(sysDictionaryTypeDb, userInfo.getAccount(), userInfo.getName());
        sysDicTypeService.save(sysDictionaryTypeDb);
        return ResponseResult.success();
    }
}
